package edu.usx.server.controller;


import edu.usx.server.pojo.*;
import edu.usx.server.service.ISitesService;
import edu.usx.server.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import edu.usx.server.pojo.Sites;
import edu.usx.server.service.ISitesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Usx
 * @since 2024-07-18
 */
@RestController
@RequestMapping("/sites")
@RequiredArgsConstructor
public class SitesController {
    private final ISitesService iSitesService;
    private final IUserService iUserService;

    @GetMapping("/link")
    public RespBean getLinkByUserId(Principal principal) {
        if (principal == null) {
            return null;
        }

        String username = principal.getName();
        //根据用户名获取完整用户对象
        User user = iUserService.getUserByUserName(username);
        user.setPassword(null);

        return RespBean.success("allLink", iSitesService.getLink(user.getId()));
    }

    @PostMapping("/link")
    public RespBean addLinkByUserId(@RequestBody Sites sites, Principal principal) {
        if (principal == null) {
            return null;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));


        LocalDateTime localDateTime = LocalDateTime.now();

        String username = principal.getName();
        //根据用户名获取用户完整对象
        User user = iUserService.getUserByUserName(username);
        user.setPassword(null);

        sites.setCreateUser(user.getId());
        sites.setUpdateUser(user.getId());
        sites.setCreateTime(localDateTime);
        sites.setUpdateTime(localDateTime);

        if (iSitesService.addLink(sites, user.getId())) {
            return RespBean.success("add success!");
        } else {
            return RespBean.error("add failed!");
        }

    }

    /**
     * @description: 获取全部服务列表
     * @author: Elapse
     * @date: 2024/7/23
     * @return: RespBean
     **/
    @ApiOperation("获取全部服务列表")
    @GetMapping("")
    public RespBean getSiteList() {
        return RespBean.success("成功获取服务列表", iSitesService.getSiteVOList());
    }

    /**
     * @description: 获取服务信息
     * @author: Elapse
     * @date: 2024/7/23
     * @return: RespBean
     **/
    @ApiOperation("获取服务信息")
    @GetMapping("/info")
    public RespBean getSiteInfo(@ApiParam("服务信息") Integer id) {
        return RespBean.success("成功获取服务信息", iSitesService.getSiteVOById(id));
    }

    /**
     * @description: 更新服务信息
     * @author: Elapse
     * @date: 2024/7/18
     * @return: RespBean
     **/
    @ApiOperation("更新服务信息")
    @PutMapping("/info/update")
    public RespBean updateSiteInfo(@ApiParam("服务信息") @RequestBody Sites site) {
        return iSitesService.updateSiteInfo(site);
    }

    /**
     * @description: 审核服务状态（管理员）
     * @author: Elapse
     * @date: 2024/7/19
     * @return: RespBean
     **/
    @ApiOperation("审核服务状态（管理员）")
    @PutMapping("/status")
    public RespBean updateSiteStatus(@ApiParam("服务信息") @RequestBody Sites site) {
        return iSitesService.updateSiteStatus(site);
    }

    @Autowired
    private ISitesService sitesService;
    @ApiOperation(value="服务搜索")
    @PostMapping("/search/list")
    public RespBean searchServices(@RequestBody PageQueryDTO request) {
        PageQueryDTO key=new PageQueryDTO(request.getKeyword(), request.getPagenum(), request.getPagesize());
        return sitesService.Search(key);
    }
}
